<?php

namespace App\Http\Controllers\Api\Home;

use App\Handlers\help;
use App\Http\Controllers\Api\Controller;
use App\Models\Give;
use App\User;
use Illuminate\Http\Request;


class ShouchongController extends Controller
{

    //status 判断是否过期 1未过期 0过期
    //is_has 是否领取   0未领取 1领取
    protected $giving = [
        ['day'=>1,'z_m'=>12,'status'=>'1','is_has'=>'0'],
        ['day'=>2,'z_m'=>8,'status'=>'1','is_has'=>'0'],
        ['day'=>3,'z_m'=>3,'status'=>'1','is_has'=>'0'],
        ['day'=>4,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>5,'z_m'=>10,'status'=>'1','is_has'=>'0'],
        ['day'=>6,'z_m'=>3,'status'=>'1','is_has'=>'0'],
        ['day'=>7,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>8,'z_m'=>10,'status'=>'1','is_has'=>'0'],
        ['day'=>9,'z_m'=>3,'status'=>'1','is_has'=>'0'],
        ['day'=>10,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>11,'z_m'=>10,'status'=>'1','is_has'=>'0'],
        ['day'=>12,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ['day'=>13,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>14,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ['day'=>15,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>16,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ['day'=>17,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>18,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ['day'=>19,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>20,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ['day'=>21,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>22,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ['day'=>23,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>24,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ['day'=>25,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>26,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ['day'=>27,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>28,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ['day'=>29,'z_m'=>4,'status'=>'1','is_has'=>'0'],
        ['day'=>30,'z_m'=>2,'status'=>'1','is_has'=>'0'],
    ];

    //用户注册后调用该方法
    public function store(Give $give){
        //todo 用户登录的id 或OPENID  这里模拟用户id为1
        $give->user_id = 1;
        $give->money_day= json_encode($this->giving);

        $give->save();

        return $this->response->array(['添加成功'])->setStatusCode(201);
    }

    public function index(){
        //TODO 获取登录用户  模拟id为1
        $user = User::find(1);
        $give = $user->give;
        if (!$give){
            return $this->response->errorNotFound('服务器异常，未获取赠送数据');
        }

        //计算实际相差天数
        $now_day = help::diff_date($give->add_time,date('Y-m-d',time()))+1;

        //判断今天之前赠币是否过去
        //更改过期状态 status 1->0
        $money_day = json_decode($give->money_day);
        if ($now_day !=1){
            //条件
            $res = $now_day-1;
            for ($i=0;$i<$now_day;$i++){
                $money_day[$i]->status = 0;
            }
        }
        $give->money_day = json_encode($money_day);
        $give->save();



        return $this->response->array([$money_day])->setStatusCode(200);
    }


    //充值
    public function star(){
        //todo 模拟用户id为1
        $user = User::find(1);
        if ($user->give->status != 1){
            return $this->response->array(['message'=>'您已经参加过活动哦'])->setStatusCode(201);
        }
        //TODO  调用充值接口   逻辑未写
        $res = true;
        //充值成功
        if ($res){
            //更新give活动表中 用户参与时间
            $user->give->add_time = date('Y-m-d',time());

            $user->give->save();
            return $this->response->array(['message'=>'记得每天来领取赠币哦'])->setStatusCode(201);
        }else{
            return $this->response->error('充值失败',404);
        }

    }


    public function getMoney(Request $request){
        //TODO 获取微信登录用户  模拟当前用户id1
        $user = User::find(1);
        //获取用户的充值50送188的信息
        $give = $user->give;
        $status = $give->status;
        if ($status == 1){
            return $this->response->errorNotFound('该用户还未参与活动');
        }elseif($status == 0){
            return $this->response->errorNotFound('该用户已参与过活动');
        }elseif($status == 2){
            //接收领取天数 day
            if (!empty($res_day=$request->input('day'))){
                //计算实际相差天数
                $now_day = help::diff_date($give->add_time,date('Y-m-d',time()))+1;

                if ($now_day == $res_day){
                    //获取json数据money_day
                    $money_day =json_decode($give->money_day);
                    //获取天数的数据
                    $data =  $money_day[$now_day-1];

                    if ($data->status == 0){
                        return $this->response->errorNotFound('已经过期了，明天记得来领哦');
                    }

                    if ($data->is_has){
                        return $this->response->errorNotFound('今天已经领取');
                    }
                    //用户添加赠币

                    $user->z_money = $user->z_money+$data->z_m;
                    $res = $user->save();
                    //更改json对应数据    对应天数的is_has  是否领取
                    $money_day[$now_day-1]->is_has = 1;
                    //更新give数据表  TODO   模拟user_id为1的用户
                    $user->give->money_day = json_encode($money_day);
                    $user->give->save();
                    return $this->response->array(['message'=>'领取成功'])->setStatusCode(200);


                }else{
                    return $this->response->errorBadRequest('请如实领取，谢谢');
                }
            }else{
                return $this->response->errorNotFound('参数异常');
            }
        }
    }
}
